<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="start">Start</button>
    <button id="cancel">Cancel</button>

    <script defer>
        class CancelToken {
          constructor(cancelFn) {
            this.promise = new Promise(resolve => {
              cancelFn(() => {
                setTimeout(console.log, 0, 'delay cancelled');
                resolve();
              });
            });
          }
        }

        const startButton = document.querySelector('#start');
        const cancelButton = document.querySelector('#cancel');

        function cancellableDelayedResolve(delay) {
          setTimeout(console.log, 0, 'set delay');
          return new Promise((resolve, reject) => {
            const id = setTimeout(() => {
              setTimeout(console.log, 0, 'delayed resolve');
              resolve();
            }, delay);

            const cancelToken = new CancelToken(cancelCallback => {
              cancelButton.addEventListener('click', cancelCallback);
            });
            cancelToken.promise.then(() => clearTimeout(id));
          });
        }

        startButton.addEventListener('click', () => cancellableDelayedResolve(1000));
    </script>
</body>

</html>